/*
TO DO:

*/

#include "Menu.h"
#include <iostream>
#include <fstream>
#include <vector>
#include <cstdlib>
#include <sstream>
#include "Book.h"
#include "Librarian.h"
#include "LibraryMember.h"
#include "Student.h"
#include "Professor.h"
#include "Guest.h"
#include "Other.h"

using namespace std;


void Menu::ManageBook(string MenuType)
{
    if( MenuType == "new" ) // uj konyv felvetele
    {
        cout << endl <<  "-- New book --" << endl;
        string Author, Title, Publisher, ISBN;
        int Year, Edition;
        char ch;
        ClearCin();
        cout << "Enter the details!" << endl << endl;
        cout << "Author: "; getline(cin, Author);
        cout  << "Title: "; getline(cin, Title);
        cout  << "Publisher: "; getline(cin , Publisher) ;
        Year = GetInt("Year: ");
        Edition = GetInt("Edition: ");
        ClearCin();
        cout  << "ISBN: "; getline(cin , ISBN) ;
        ClearCin();
        Book* tmp = new Book(Author, Title, Publisher, Year, Edition, ISBN);
        cout << endl;
        (tmp)->ListBook();
        cout << endl;
        cout << "Create new book with these details? (y = yes, everything else = no) " << endl;
        cout << "> ";
        cin >> ch;
        if( ToLower(ch) != 'y' )
        {
            cout << "Creating canceled!" << endl;
            delete tmp;
        }
        else
        {
            Books.push_back(tmp);
            cout << "Creating done!" << endl;
        }
    }
    if( Books.size()>0 )
    {
        if( MenuType == "list" )
        {
            cout << endl;
            for( unsigned int i=0; i<Books.size(); ++i )
            {
                Books[i]->ListBook();
                cout << endl;
            }
            cout<< endl;
        }
        if( MenuType == "delete" )
        {
            char ch;
            cout << endl << "-- Delete book --" << endl;
            ManageBook("list"); // kilistazza a konyveket
            ClearCin();
            string BookIDToDelete; // a torolni kivant konyv ID-ja
            int BookID_i; // a torlesre varo konyv indexe a konyvek vektoraban
            cout << "BookID to delete: " << endl;
            cout << "> ";
            getline(cin, BookIDToDelete);

            for( unsigned int i=0; i<Books.size(); ++i) // megkeressuk, hogy mi az indexe a vektorban
            {
                if( (Books[i])->GetBookID() == BookIDToDelete)
                {
                    BookID_i = i;
                }
            }

            if( RealBookID(BookIDToDelete,false) ) // ha helyes a konyv azonosito
            {
                Books[BookID_i]->ListBook(); // kiirjuk a konyv adatait
                if( !Books[BookID_i]->GetLoanable() ) // ellenorizzuk, hogy ki van-e adva a konyv
                {
                    cout << endl << endl << "Loaned book! Return it before delete! Return it now to delete? (y = yes, everything else = no) " << endl;
                    cout << "> ";
                    cin >> ch;
                    if( ToLower(ch) == 'y' )
                    {
                        try
                        {
                            Return( Books[BookID_i]->GetLoaner() ); // ha ki van adva, es torolni akarjuk, akkor  elobb visszavesszuk
                        }catch(LibraryMember::Exception ex){cout << "Error! Book will be not deleted!" << endl;}
                    }
                    else // ha torolni akartuk, ki van adva, de nem vesszuk vissza --> igy nem lehet torolni a konyvet
                    {
                        cout << "Deleting canceled!" << endl;
                    }
                }
                if( Books[BookID_i]->GetLoanable() ) // ha nincs kolcsonozve a konyv
                {
                    cout << "Delete this book? (y = yes, everything else = no) " << endl;
                    cin >> ch;
                    if( ToLower(ch) != 'y' )
                    {
                        cout << "Deleting canceled!" << endl;
                    }
                    else // torolhetjuk a konyvet
                    {
                        delete Books[BookID_i];
                        Books.erase(Books.begin()+BookID_i);
                        cout << "Deleting done!" << endl;
                    }
                }
            }
            else // hibas konyv azonosito
            {
                cout << "Wrong BookID! Deleting canceled!" << endl;
            }
        }
    }
    else // nem lehet konyvet torolni
    {
        cout << "You can not delete book!" << endl;
    }
}


void Menu::SearchBook()
{
    cout << endl << "-- Search book --" << endl;
    cout << "Searching types: (1 - Author, 2 - Title, 3 - ISBN, 4 - BookID)" << endl;
    int searchtype, db;
    ClearCin();
    searchtype = GetInt("Choose searching type: ");
    string mitmit;
    ClearCin();
    cout << "What are you looking for?" << endl;
    cout << "> ";
    cin >> mitmit;
    cout << endl;
    if( Books.size()>0 ) // ha mar van konyv felveve
    {
        db = 0;
        for( unsigned int i = 0; i<Books.size(); ++i)
        {
           if( Books[i]->SearchBook(searchtype, mitmit) )
           {
               Books[i]->ListBook();
               cout << endl;
               db++;
            }
        }
        cout << db << " found(s)." << endl;
        if( db == 0 ) // ha nincs talalat a keresesre
        {
            cout << "No result!" << endl;
        }
    }
    else // nincs konyv se
    {
        cout << "No book in!" << endl;
    }
}


void Menu::ManageMember(string MenuType)
{
    if( MenuType == "new" ) // uj tag felvetele
    {
        cout <<endl << "-- New member --" << endl;
        string Name, Address, Availability;
        char vsz;
        int mtype;
        ClearCin();
        cout << "Enter the new member details: " << endl;
        cout << "Name: "; getline(cin, Name);
        cout  << "Address: "; getline(cin, Address);
        cout  << "Availability: "; getline(cin , Availability) ;
        cout << "MemberType: (1 - Student, 2 - Professor, 3 - Guest, 4 - Other)" << endl;
        cout << "Default type is: 4 - Other!"<< endl;
        mtype = GetInt("Choose: ");
        LibraryMember* tmp;
        if (mtype == 1) { tmp = new Student(Name, Address, Availability); } // hallgato
        else if (mtype == 2) { tmp = new Professor(Name, Address, Availability); } // tanar
        else if (mtype == 3) { tmp = new Guest(Name, Address, Availability); } // mas egyetemrol
        else { tmp = new Other(Name, Address, Availability); } // egyeb tag
        /***/
        cout << endl;
        (tmp)->ListMember();
        cout << endl;
        cout << "Create new member with these details? (y - yes / n - no) " << endl;
        cout << "> ";
        cin >> vsz;
        if( ToLower(vsz)!='y' )
        {
            cout << "Canceled!" << endl;
            delete tmp;
        }
        else
        {
            LibraryMembers.push_back(tmp);
            cout << "New member done!" << endl;
        }
    }
    if(LibraryMembers.size()!=0) //ha mar van tag, akkor lehet veluk mit csinalni
    {
        if( MenuType == "list" ) // kilistazas
        {
            cout << endl;
            for( unsigned int i=0; i<LibraryMembers.size(); ++i ) // osszes tag listazasa
            {
                LibraryMembers[i]->ListMember();
                cout << endl;
            }
        }
        if( MenuType == "delete" ) // torles
        {
            char vsz;
            cout << "-- Delete member --" << endl;
            ManageMember("list"); // ki is listazzuk a tagokat biztos ami biztos
            //cout << "Enter MemberID to delete: " << endl; // TODO
            ////
            ClearCin();
            int memID;
            memID = GetInt("Enter MemberID to delete: ");
            cout << endl;
            ClearCin();
            unsigned int memind;
            /***/
            for( memind=0; memind<LibraryMembers.size(); ++memind)
            {
                if( memID == (LibraryMembers[memind])->GetLibraryMemberID() )
                {
                    break;
                }
            }
            ////
            if( RealMemberID(memID) )
            {
                //LibraryMembers[memind]->ListMember(); // a torlendo tag kilistazasa // nem reszletes kiiras ///////////////DEBUG//
                LibraryMembers[memind]->MemberDetails(); // a torlendo tag kilistazasa
                if( !LibraryMembers[memind]->GetNumOfLoans() == 0 ) // ellenorizzuk, hogy nincs-e kintlevo konyve
                {
                    cout << endl << "Loanings are there! Return the loaned books to this member?" << endl << "y - yes, n - no : " << endl;
                    cout << "> ";
                    ClearCin();
                    cin >> vsz;
                    if( ToLower(vsz) == 'y' ) // visszavesszuk a kintlevo konyveket
                    {
                        try
                        {
                            cout << "Returning loans..." << endl << endl;
                            int numofloans = LibraryMembers[memind]->GetNumOfLoans();
                            for( int j=0; j<numofloans; ++j )
                            {
                                cout << LibraryMembers[memind]->FirstLoan()->GetBookID() << " BookID is returning...";
                                int delay = Return(LibraryMembers[memind]); // itt lenne a keses kiirasa...
                                if( delay>0 )
                                {
                                    cout <<endl << "DELAY!"<< endl << delay << " day delay!" << endl;
                                }
                                cout << "Returning complete." << endl;
                            }
                            cout << endl << "Alright!" << endl;
                        }catch(LibraryMember::Exception ex){cout << "Error with returning! Deleting canceled!" << endl;}
                    }
                    else // ha nem vesszuk vissza a konyveket --> nem lehet torolni sem
                    {
                        cout << "Error with returning! Deleting canceled!" << endl;
                    }
                }
                if( LibraryMembers[memind]->GetNumOfLoans() ==0 ) // ha mar nincs kintlevosege, akkor torolhetjuk is
                {
                    cout << endl;
                    cout << "Delete member? (y - yes) " << endl;
                    cout << "> ";
                    cin >> vsz;
                    if( ToLower(vsz) != 'y' )
                    {
                        cout << "Canceled!" << endl;
                    }
                    else
                    {
                        delete LibraryMembers[memind];
                        LibraryMembers.erase(LibraryMembers.begin()+memind);
                        cout << "Member deleted!" << endl;
                    }
                }
            }
            else
            {
                cout << "Error! No MemberID like that! Deleting canceled!" << endl;
            }
        } // a torlesnek vege
        if( MenuType == "edit" ) // tag modositasa
        {
            cout << endl << "-- List member details --" << endl;
            ManageMember("list"); // tagok kilistazasa
            ////
            ClearCin();
            int memID;
            memID = GetInt("Concrete MemberID to list details: ");
            cout << endl;
            ClearCin();
            unsigned int memind;
            /***/
            for( memind=0; memind<LibraryMembers.size(); ++memind)
            {
                if( memID == (LibraryMembers[memind])->GetLibraryMemberID() )
                {
                    break;
                }
            }
            ////
            if( RealMemberID(memID) )
            {
                LibraryMembers[memind]->MemberDetails();
                MenuID = memind; // eltaroljuk a menu privat tagjakent az indexet
            }
            else
            {
                cout << "Missing MemberID! List Canceled!" << endl;
            }
        }
        if( MenuType == "write" ) /////////////////////////////////////////
        {
            if( !( MenuID<LibraryMembers.size() ) )
            {
                cout << "Choose ID 3!" << endl;
                ManageMember("edit");
            }
            else
            {
                int vsz = 0;
                char v;
                string Name, Address, Availability, mod;
                Name = LibraryMembers[MenuID]->GetName();
                Address = LibraryMembers[MenuID]->GetAddress();
                Availability = LibraryMembers[MenuID]->GetAvailability();
                ClearCin();
                cout << endl << "-- Modify Member --" << endl;
                cout << endl;
                LibraryMembers[MenuID]->MemberDetails();
                cout << endl;
                cout << "Which detail to modify? 1 - Name, 2 - Address, 3 - Availability, 4 - MemberType" << endl;
                vsz = GetInt("Choice: ");
                ClearCin();
                switch(vsz)
                {
                    case 1:
                        cout << "New name: ";
                        getline(cin, mod);
                        ClearCin();
                        cout << "Right? (" << Name << "->" << mod << ") y - yes " << endl;
                        cout << "> ";
                        cin >> v;
                        ClearCin();
                        break;
                    case 2:
                        cout << "New address: ";
                        getline(cin, mod);
                        ClearCin();
                        cout << "Right? (" << Address<< "->" << mod << ") y - yes " << endl;
                        cout << "> ";
                        cin >>v;
                        ClearCin();
                        break;
                    case 3:
                        cout << "New availability: ";
                        getline(cin, mod);
                        ClearCin();
                        cout << "Right? (" <<Availability << "->" << mod << ") y - yes " << endl;
                        cout << "> ";
                        cin >>v;
                        ClearCin();
                        break;
                    case 4:
                        cout << "New membertype: " << endl << "(1 - Student, 2 - Professor, 3 - Guest, 4 - Other) " << endl;
                        cout << "> ";
                        getline(cin, mod);
                        ClearCin();
                        cout << "Right? y - yes " << endl;
                        cout << "> ";
                        cin >> v;
                        ClearCin();
                        break;
                }
                if( vsz >= 1 && vsz <= 4 && ToLower(v) == 'y' ) // ha helyesen akar modositani es megerositette
                {
                    if( vsz != 4 )
                    {
                        LibraryMembers[MenuID]->ModifyMember(vsz, mod);
                        cout << "Modification done!" << endl;
                    }
                    else // tipus valtoztatas --> uj tag az uj tipus alapjan es atrakjuk a regibol az ujba az adatait
                    {
                        if( LibraryMembers[MenuID]->GetNumOfLoans() == 0 )
                        {
                            LibraryMember* tmp;
                            int memtype = atoi(mod.c_str()); // string to int konvertalas
                            if (memtype == 1) { tmp = new Student(Name, Address, Availability); } // hallgato
                            else if (memtype == 2) { tmp = new Professor(Name, Address, Availability); } // tanar
                            else if (memtype == 3) { tmp = new Guest(Name, Address, Availability); } // mas egyetemrol
                            else { tmp = new Other(Name, Address, Availability); } // egyeb tag
                            LibraryMembers.erase(LibraryMembers.begin()+MenuID);
                            LibraryMembers[MenuID] = tmp;
                            cout << "Modification complete!" << endl;
                        }
                        else // ha van kintlevosege, akkor a modositas elott vissza kell venni
                        {
                            cout << endl << "LOANINGS! Return them to modify membertype!" << endl;
                        }
                    }
                }
                else // valamit rosszul akart valtoztatni, vagy megse-zte vegul
                {
                    cout << endl << "Modification canceled!" << endl;
                }
            LibraryMembers[MenuID]->MemberDetails();
            }
        }
    }
    else // ha nincs tag, akkor nincs mit csinalni vele
    {
        cout << "No member!" << endl;
    }
}


void Menu::Loan() // kolcsonzest vegzo eljaras
{
    cout << endl << "-- Loan --" << endl;
    if( LibraryMembers.size() != 0 && Books.size() != 0 ) // ha van tag es van konyv is
    {
        cout << "Members: " << endl;
        ManageMember("list");
        char ch;
        ////
        ClearCin();
        int memID;
        memID = GetInt("Enter MemberID to loan: ");
        ClearCin();
        int memind;
        /***/
        for( memind=0; memind<(int)LibraryMembers.size(); ++memind)
        {
            if( memID == (LibraryMembers[memind])->GetLibraryMemberID() )
            {
                break;
            }
        }
        ////
        if( RealMemberID(memID) ) // ha valos a tag azonosito
        {
            cout << endl;
            cout << "Books: " << endl;
            cout << endl;
            for( unsigned int i=0; i<Books.size(); ++i )
            {
                Books[i]->ListBook();
                cout << endl;
            }
            string real_bookid_str;
            ClearCin();
            cout << "Enter BookID to loan: " << endl;
            cout << "> ";
            cin >> real_bookid_str;
            ClearCin();
            if( GetLoanableBooks()>0 ) // ha van kolcsonozheto konyv
            {
                if( RealBookID(real_bookid_str,false) ) // ha valos a konyv azonosito
                {
                    int BookID_i;
                    for( unsigned int i=0; i<Books.size(); ++i) // megkeressuk, hogy mi az indexe a vektorban
                    {
                        if( (Books[i])->GetBookID() == real_bookid_str)
                        {
                            BookID_i = i;
                        }
                    }
                    if( Books[BookID_i]->GetLoanable() ) // ha kolcsonozheto a konyv
                    {
                        LibraryMembers[memind]->MemberDetails();
                        cout << endl << "Book details: "<< endl << endl;
                        Books[BookID_i]->ListBook();
                        if( !LibraryMembers[memind]->CanLoan() ) // ha mar nem tud tobbet kolcsonozni a tag
                        {
                            cout << endl << endl << "Member reached the maximum number of loans!" << endl;
                        }
                        else
                        {
                            cout << endl << "Accept loaning? (y = yes, everything else = no) " << endl;
                            ClearCin();
                            cout << "> ";
                            cin >> ch;
                            if( ToLower(ch) != 'y' )
                            {
                                cout << "Loaning canceled!" << endl;
                            }
                            else
                            {
                                if( !( LibraryMembers[memind]->Loan(Books[BookID_i]) ) )
                                {
                                    cout << "Member reached the maximum number of loans!";
                                }
                                else
                                {
                                    cout << "Loaning done!" << endl;
                                    //ManageBook("list");
                                }
                            }
                        }
                    }
                    else // a konyv mar ki van kolcsonozve
                    {
                        cout << "This book is already loaned!" << endl;
                    }
                }
                else // hibas konyv azonosito
                {
                    cout << "Error! Wrong BookID!" << endl;
                }
            }
            else
            {
                cout << "No loanable book in!" << endl;
            }
        }
        else // hibas tag azonosito
        {
            cout << "Error! Wrong MemberID!" << endl;
        }
    }
    else // ha nincs konyv
    {
        cout << "Error! Missing Book or Member!" << endl;
    }
}


void Menu::Return() // visszavetelt vegzo eljaras
{
    cout << endl << "-- Return --" << endl;
    cout << "BookID to return: " << endl;
    if( LibraryMembers.size() != 0 && Books.size() != 0 && GetLoanableBooks(false)>0 ) // ha van tag es konyv es meg kolcsonzott konyv is
    {
        char ch;
        cout << "Choose BookID: " <<endl;
        cout << "> ";
        string real_bookid_str;
        cin >> real_bookid_str;
        if( RealBookID(real_bookid_str,false) )
        {
            int BookID_i;
            for( unsigned int i=0; i<Books.size(); ++i) // megkeressuk, hogy mi az indexe a vektorban
            {
                if( (Books[i])->GetBookID() == real_bookid_str)
                {
                    BookID_i = i;
                }
            }
            if( !Books[BookID_i]->GetLoanable() ) // ha valos konyv es kolcsonzott
            {
                int delay;
                cout << "Loaner details: " << endl;
                Books[BookID_i]->GetLoaner()->MemberDetails();
                cout << "Returned BookID: " << real_bookid_str << ". Is it correct? (y = yes, everything else = no) " << endl;
                ClearCin();
                cout << "> ";
                cin >> ch;
                if( ToLower(ch) != 'y' )
                {
                    cout << "Return canceled!" << endl;
                }
                else //TODO//////////////////////////////////////////////////////////////////////////////////////////////////
                {
                    try
                    {
                        delay = Return(Books[BookID_i]->GetLoaner());
                        if( delay>0 )
                        {
                            cout <<endl << "DELAY :" << delay << " days!" << endl;
                        }
                        cout << "Return done." << endl;
                    }catch(LibraryMember::Exception ex){cout << "No book to return!" << endl;}
                }
            }
        }
        else
        {
            cout << "Error! No BookID to return!" << endl;
        }
    }
    else // ha nincs konyv
    {
        cout << "Error! Missing Book or Member!" << endl;
    }
}


int Menu::Return(LibraryMember* mitmit)
{
    int delay_return = 0;
    delay_return = (mitmit->Return(menu_private_BookID));
    return delay_return;
}


void Menu::LoadData() // betoltest vegzo eljaras
{
    // konyvek beolvasasa
    ifstream BookFile; // a konyveket tartalmazo fajl
    {
        string BookID, Author, Title, Publisher, Year, Edition, ISBN; //ideiglenes adatokat tartalmazo valtozok
        BookFile.open("book.dat");
        if( BookFile.is_open() ) // ha a fajl megnyitasa sikeres
        {
            while( BookFile.good() ) // fajlbeolvasas
            {
                stringstream line;
                string tmp_line;
                getline(BookFile,tmp_line);
                line << tmp_line;
                    getline(line, BookID, ';' ); // ID beolvasasa
                    getline(line, Author, ';' ); // szerzo beolvasasa
                    getline(line, Title, ';' ); // cim
                    getline(line, Publisher, ';' ); // kiado
                    getline(line, Year, ';' ); // kiadas eve
                    getline(line, Edition, ';' ); // hanyadik kiadas
                    getline(line, ISBN, '\n' ); // ISBN
                if( BookID!="" )
                {
                    Books.push_back(new Book(BookID, Author, Title, Publisher, atoi(Year.c_str()), atoi(Edition.c_str()), ISBN));
                }
            }
            BookFile.close();
        }
        else throw MISSING_FILE; // ha nincs meg a fajl (kivetelt dob)
    }
    // tagok beolvasasa
    ifstream LibraryMemberFile;
    {
        string TypeID, TypeName, Name, Address, Availability, LibraryMemberID, NumOfLoans, bID;
        int db_i; //id=kolcsonzott konyv id-je, db_i az osszes kolcsonzes db-szama
        int memtype;
        unsigned int bookindex;
        LibraryMemberFile.open("librarymember.dat");
        if(LibraryMemberFile.is_open()) //fajl megnyitasa sikeres?
        {
            while(LibraryMemberFile.good()) //fajlbeolvasas
            {
                stringstream line;
                string tmp_line;
                getline(LibraryMemberFile,tmp_line);
                line << tmp_line;
                    getline(line, TypeID, ';' ); // tipusID
                    getline(line, TypeName, ';' ); // tipus
                    getline(line, Name, ';' ); // nev
                    getline(line, LibraryMemberID, ';' ); // tipus
                    getline(line, Address, ';' ); // cim
                    getline(line, Availability, ';' ); // elerhetoseg
                    getline(line, NumOfLoans, ';' ); // kolcsonzesek szama
                if( TypeID != "" )
                {
                    memtype = atoi(TypeID.c_str());
                    LibraryMember* tmp;
                    if(memtype == 1)
                    {
                        tmp = new Student(Name, Address, Availability);
                    }
                    else if(memtype == 2)
                    {
                        tmp = new Professor(Name, Address, Availability);
                    }
                    else if(memtype == 3)
                    {
                        tmp = new Guest(Name, Address, Availability);
                    }
                    else
                    {
                        tmp = new Other(Name, Address, Availability);
                    }
                    db_i = atoi(NumOfLoans.c_str()); //TODO1///////////////////////////////////////////////////////////////////////
                    //cout << "DEBUG 697: " << db_i << endl;//debug
                    //cout << "DEBUG 698: " << NumOfLoans << endl;//debug
                    for( int i=1; i<=db_i; i++ )
                    {
                        getline(line, bID, ';' ); // adott kolcsonzes
                        for( bookindex=0; bookindex<LibraryMembers.size(); ++bookindex) // megkeresi az adott konyv indexet a vektorban
                        {
                            if( bID == (Books[bookindex])->GetBookID() )
                            {
                                break;
                            }
                        }
                        if( !RealBookID(bID, false) ) { throw BAD_INPUT; } // ha nincs megadott konyv --> kivetel dobasa
                        if( !tmp->Loan_LOAD(Books[bookindex]) ) { throw BAD_INPUT; }
                    }
                    LibraryMembers.push_back(tmp);
                }
            }
            LibraryMemberFile.close();
        }
        else throw MISSING_FILE; // ha nincs meg a fajl
    }
}

// valoszinuleg ezekben mar nincs hiba:

Menu::Menu()
{
    cout << "Library Administrator" << endl;
    if(!Login())
    {
        cout << "AUTH_FAILURE! exception" << endl;
        throw Menu::AUTH_FAILURE;
    }
    else
    {
        LoadData();
    }
    cout << "Login done! Data is loaded." << endl;
}


Menu::~Menu()
{
    SaveData();
    cout << "Data is saved. Press a key to exit!" << endl;
    system("pause");
    cout << "Exiting..." << endl;
}


void Menu::WriteMenu(string MenuType) const
{
    if( MenuType == "main" )
    {
        cout << endl << endl << endl;
        //system("cls");
        cout << "--- MENU ---" << endl;
        cout << "Choose a submenu!" << endl;
        cout << "\t-----------------" << endl;
        cout << "\t1 - Books:" << endl;
        cout << "\t2 - Members" << endl;
        cout << "\t3 - Loan" << endl;
        cout << "\t4 - Return" << endl;
        cout << "\t-----------------" << endl;
        cout << "\t0 - Save and exit..." << endl;
    }
    if( MenuType == "book" )
    {
        cout << endl << endl << endl;
        //system("cls");
        cout << "--- Books ---" << endl;
        cout << "\t-----------------" << endl;
        cout << "\t1 - Book list" << endl;
        cout << "\t2 - New book" << endl;
        cout << "\t3 - Delete book" << endl;
        cout << "\t4 - Search book" << endl;
        cout << "\t-----------------" << endl;
        cout << "\t0 - Return to main menu..." << endl;
    }
    if( MenuType == "member" )
    {
        cout << endl << endl << endl;
        //system("cls");
        cout << "--- Members ---" << endl;
        cout << "\t-----------------" << endl;
        cout << "\t1 - Member list" << endl;
        cout << "\t2 - New member" << endl;
        cout << "\t3 - Delete member" << endl;
        cout << "\t4 - Modify member/Member details" << endl;
        cout << "\t-----------------" << endl;
        cout << "\t0 - Return to main menu..." << endl;
    }
    if( MenuType == "det" )
    {
        cout << endl << endl << endl;
        //system("cls");
        cout << "--- Member data ---" << endl;
        cout << "\t-----------------" << endl;
        cout << "\t1 - Member details" << endl;
        cout << "\t2 - Modify member" << endl;
        cout << "\t-----------------" << endl;
        cout << "\t0 - Return to main menu..." << endl;
    }
}


void Menu::RunMenu(string MenuType)
{
    int Option = 0;
    if( MenuType == "main" )
    {
        do
        {
            WriteMenu(); // fomenu kiirasa
            Option = GetInt("> "); // valasztott menupont
            switch(Option)
            {
                case 1:
                    RunMenu("book");
                    break;
                case 2:
                    RunMenu("member");
                    break;
                case 3:
                    Loan();
                    break;
                case 4:
                    Return();
                    break;
            }
        }while( Option != 0 );
    }
    if( MenuType == "book" )
    {
        do
        {
            WriteMenu("book"); // konyv menu kiirasa
            Option = GetInt("> ");
            switch(Option)
            {
                case 1:
                    ManageBook("list");
                    break;
                case 2:
                    ManageBook("new");
                    break;
                case 3:
                    ManageBook("delete");
                    break;
                case 4:
                    SearchBook();
                    break;
            }
        }while( Option != 0 );
    }
    if( MenuType == "member" ) //tagok menu
    {
        do
        {
            WriteMenu("member"); // tagok menu kiirasa
            Option = GetInt("> ");
            switch(Option)
            {
                case 1:
                    ManageMember("list");
                    break;
                case 2:
                    ManageMember("new");
                    break;
                case 3:
                    ManageMember("delete");
                    break;
                case 4:
                    ManageMember("edit");
                    RunMenu("det");
                    break;
            }
        }while( Option != 0 );
    }
    if( MenuType == "det" )
    {
        do
        {
            WriteMenu("det"); // tag adatai menu kiirasa
            Option = GetInt("> ");
            switch(Option)
            {
                case 1:
                    ManageMember("edit");
                    break;
                case 2:
                    ManageMember("write");
                    break;
            }
        }while( Option != 0 );
    }
}


void Menu::SaveData() // adatok mentese
{
    // konyvek mentese
    ofstream BookFile; // a konyveket tartalmazo fajl
    BookFile.open("book.dat");
    if(BookFile.is_open()) // ha a fajl megnyitasa sikeres
    {
        for(unsigned int i=0; i<Books.size(); i++)
        {
            BookFile<<(Books[i]);
        }
        BookFile.close();
    }
    else throw MISSING_FILE; // ha nincs meg a fajl kivetel dobas

    // tagok mentese
    ofstream LibraryMemberFile; // a tagokat tartalmazo fajl
    LibraryMemberFile.open("librarymember.dat");
    if(LibraryMemberFile.is_open()) // ha a fajl megnyitasa sikeres
    {
        for(unsigned int i=0; i<LibraryMembers.size(); i++)
        {
            LibraryMemberFile<<(LibraryMembers[i]);
            if(i+1<LibraryMembers.size())
            {
                LibraryMemberFile<<endl;
            }
        }
	LibraryMemberFile.close();
    }
    else throw MISSING_FILE; // ha nincs meg a fajl kivetel dobas
}


bool Menu::Login()
{
    try
    {
        cout << "Please login!" << endl;
        bool ok = false;
        Librarian lib;
        ok = lib.Login();
        return ok;
    }catch(Librarian::Exception ex){throw BAD_AUTHFILE;}
}


bool Menu::RealBookID(const string& realbookid, bool which_RealBookID)
{
    unsigned int i;
    if(which_RealBookID) // ha bekerjuk az adatot
    {
        ClearCin();
        menu_private_BookID = "";
        cout << "Choose ID 5: " << endl;
        cout << "> ";
        getline(cin, menu_private_BookID);
    }
    else // ha parameterkent adjuk meg
    {
        menu_private_BookID = realbookid;
    }
    for( i=0; i<Books.size(); ++i )
    {
        if( menu_private_BookID == (Books[i])->GetBookID() )
        {
            break;
        }
    }
    return i<Books.size();
}


bool Menu::RealMemberID(int& i)
{
    ClearCin();
    MenuID = 0;
    //MenuID = GetInt("Choose ID 6: ");
    MenuID = i;
    i = 0;
    for( i=0; i<(int)LibraryMembers.size(); ++i )
    {
        if( (int)MenuID == (LibraryMembers[i])->GetLibraryMemberID() )
        {
            break;
        }
    }
    return i<(int)LibraryMembers.size();
}


int Menu::GetLoanableBooks(bool loanable) //Speclist()
{
    int db = 0;
    cout << endl;
    //Books[0]->ListBook(); // ezt is most kiszedem hatha ez listaz ki kozben
    for( unsigned int i=0; i<Books.size(); ++i )
    {
        if( Books[i]->GetLoanable() == loanable )
        {
            db++;
            //Books[i]->ListBook(); // ezt most kiszedem, hatha ez listaz ki kozben
        }
    }
    if( db == 0 )
    {
        cout << "ERROR! No book like that" << endl;
    }
    cout<< endl;
    return db;
}


int Menu::GetInt(const string& msg) const
{
	int number;
	bool error;
	do
	{
		cout << msg;
		cin >> number;
		if( cin.fail() )
		{
			cin.clear();
			cin.ignore(256, '\n');
			cout << "Wrong input!" << endl;
			error = true;
        }
		else
            error = false;
    }
    while(error);
	return number;
}


void Menu::ClearCin() const // cin tisztitasa, mert hasznalunk cin-t es getline-t is...
{
    cin.clear();
    cin.sync();
}


char Menu::ToLower(char mitmit) // karakter kisbetuve konvertalasa
{
    char mitmit_return;
    mitmit_return = std::tolower(mitmit);
    return mitmit_return;
}

